/*********************************************************************
 *                                                                   *
 *  Js Css Minifier ( JsCssMin )                                     *
 *  Author: Igor Kanshyn                                             *
 *  Copyright (c) 2009, Igor Kanshyn. All rights reserved.           *
 *                                                                   *
 *  JsCssMin is released under the Apache Software License.          *
 *  See LICENSE.txt for more details.                                *
 *                                                                   *
 *********************************************************************/

package org.grenader.jscssmin.integrations;


import org.grenader.jscssmin.ProcessLogger;
import org.inconspicuous.jsmin.JSMin;

import java.io.*;

/**
 * <p>Product: JsCssMin ( JavaScript and CSS Minifier )<br>
 * Author: Igor Kanshyn (grenader).
 * </p>
 * Date: May 1, 2009
 */

public class JsMinIntegration {

    public static boolean processResourceFile(File resourceFile, File tmpResultFile, ProcessLogger errorLogger) {
        FileInputStream inputStream = null;
        FileOutputStream outputStream = null;
        try {
            inputStream = new FileInputStream(resourceFile);
            outputStream = new FileOutputStream(tmpResultFile);

            JSMin jsmin = new JSMin(inputStream, outputStream);
            jsmin.jsmin();

            return true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
//            errorLogger.addErrorMessage(resourceFile.getAbsolutePath());            
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return true;
        }
        finally {
            if (outputStream != null)
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    return false;
                }
            if (inputStream != null)
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    return false;
                }
        }
    }
}
